/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.ondoc.beans;

import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * 设置了表示记录是否有效的常量
 *
 * @author jansing lz_jansing@163.com
 */
@Entity
@Table(name = "t_document")
public class Document {

    public static final int IS_VAILD = 1;
    public static final int IS_NOT_VAILD = 0;

    private int id;
    private String name;
    private User upUser;
    /**
     * 存档地址，默认服务器下，
     */
    private String path;
    /**
     * 该文档被下载数量
     */
    private int downtotal;
    private Date upDate;
    /**
     * 该文档属于哪个分类
     */
    private Category category;
    /**
     * 简介
     */
    private String info;
    /**
     * 浏览量、点击量
     */
    private int views;
    /**
     * 评论数
     */
    private int comCount;
    /**
     * 所有评论的平均星级
     */
    private int stars;
    /**
     * 1：有效，0：无效
     */
    private int isVaild;
    /**
     * 该文档下的评论
     */
    private Set<Comment> comSet;

    /**
     * isVaild、comSet属性已初始化，<br/>
     * info可以为空，<br/>
     * 其他属性都要初始化<br/>
     */
    public Document() {
        this.isVaild = IS_VAILD;
        comSet = new HashSet<>();
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name = "u_id")
    @Cascade(CascadeType.SAVE_UPDATE)
    @Fetch(FetchMode.JOIN)
    public User getUpUser() {
        return upUser;
    }

    public void setUpUser(User upUser) {
        this.upUser = upUser;
    }

    /**
     * 该文档被下载数量
     */
    @Column(name = "down_total")
    public int getDowntotal() {
        return downtotal;
    }

    /**
     * 该文档被下载数量
     */
    public void setDowntotal(int downtotal) {
        this.downtotal = downtotal;
    }

    @Column(name = "up_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getUpDate() {
        return upDate;
    }

    public void setUpDate(Date upDate) {
        this.upDate = upDate;
    }

    /**
     * 该文档属于哪个分类<br/>
     * 因为只有在浏览文档的时候才要它的分类，所以懒加载
     *
     * @return
     */
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Category.class)
    @JoinColumn(name = "c_id")
    @Cascade(CascadeType.SAVE_UPDATE)
    public Category getCategory() {
        return category;
    }

    /**
     * 该文档属于哪个分类
     */
    public void setCategory(Category category) {
        this.category = category;
    }

    /**
     * 简介
     */
    @Column(name = "info")
    public String getInfo() {
        return info;
    }

    /**
     * 简介
     */
    public void setInfo(String info) {
        this.info = info;
    }

    /**
     * 浏览量、点击量
     */
    @Column(name = "views")
    public int getViews() {
        return views;
    }

    /**
     * 浏览量、点击量
     */
    public void setViews(int views) {
        this.views = views;
    }

    /**
     * 评论数
     */
    @Column(name = "com_count")
    public int getComCount() {
        return comCount;
    }

    /**
     * 评论数
     */
    public void setComCount(int comCount) {
        this.comCount = comCount;
    }

    /**
     * 所有用户的评论的平均星级
     *
     * @return
     */
    @Column(name = "stars")
    public int getStars() {
        return stars;
    }

    /**
     * 所有用户的评论的平均星级
     *
     * @param stars
     */
    public void setStars(int stars) {
        this.stars = stars;
    }

    /**
     * 1：有效，0：无效
     *
     * @return
     */
    @Column(name = "is_vaild")
    public int getIsVaild() {
        return isVaild;
    }

    /**
     * 1：有效，0：无效
     *
     * @param isVaild
     */
    public void setIsVaild(int isVaild) {
        this.isVaild = isVaild;
    }

    /**
     * 该文档下的评论<br/>
     * 因为只有在浏览文档的时候才要加载它的评论，所以懒加载
     *
     * @return
     */
    @OneToMany(mappedBy = "document", fetch = FetchType.LAZY, cascade = {javax.persistence.CascadeType.REMOVE})
    public Set<Comment> getComSet() {
        return comSet;
    }

    /**
     * 该文档下的评论
     */
    public void setComSet(Set<Comment> comSet) {
        this.comSet = comSet;
    }

    /**
     * 存档地址
     */
    @Column(name = "path")
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.name);
        hash = 67 * hash + Objects.hashCode(this.upUser);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Document other = (Document) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        if (!Objects.equals(this.upUser, other.upUser)) {
            return false;
        }
        if (!Objects.equals(this.upDate, other.upDate)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String s = null;
        try{
            s = "Document{" + "id=" + id + ", name=" + name + ", upUser=" + upUser.getUsername() + ", downtotal=" + downtotal + ", upDate=" + upDate + ", path=" + path + ", info=" + info + ", views=" + views + ", comCount=" + comCount + ", stars=" + stars + ", isVaild=" + isVaild + '}';
        } catch(Exception e){
            System.err.println("Document实体类的toString错误，请查看源码");
        }
        return s;
    }

}
